﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Infrastructure;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.Infrastructure.Authentication;

namespace XSTDZ.ERP.WebApp.Controllers
{
    [NoAuthorizated]
    public class OperationController : Controller
    {
        private readonly IPermisstionService permissionServiceImpl = ServiceLocator.Instance.GetService<IPermisstionService>();	
        //
        // GET: /Operation/
        
        //操作列表
        public ActionResult Index()
        {
            return View();
        }
        #region 添加操作
       
        [HttpGet]
        public ActionResult Add(Guid? operationId)
        {
            Guid parentid;
            if (operationId != null && !Guid.Empty.Equals(operationId))
            {
                Guid.TryParse(operationId.ToString(), out parentid);
                var operation = permissionServiceImpl.GetOperation(parentid);
                ViewBag.ParentName = operation.OperationName;
                ViewBag.ParentId = operation.Id;
            }
            if (ViewBag.ParentId == null)
            {
                ViewBag.ParentName ="";
                ViewBag.ParentId ="";
            }
            return View();
        }
        [HttpPost]
        public ActionResult Add(OperationDto operation)
        {
            if (ModelState.IsValid)
            {
                var parentId=Request.Form["ParentID"];
                if (!string.IsNullOrEmpty(parentId))
                {
                    operation.ParentId =Guid.Parse( parentId);
                }
                permissionServiceImpl.Add(operation);
                ViewBag.PostState = 9;
            }
            return View();
        }
        #endregion
        #region 更新操作
        [HttpGet]
        public ActionResult Edit(int type,Guid operationId)
        {
            ViewBag.IsParent = type==1?false:true;//0:父级菜单 1:子菜单
            var operation = permissionServiceImpl.GetOperation(operationId);
            return View(operation);
        }
        [HttpPost]
        public ActionResult Edit(OperationDto operation)
        {
            var isparent = Request.Form["IsParent"];
            if (ModelState.IsValid)
            {
                permissionServiceImpl.Update(operation);
                ViewBag.PostState = 9;
            }
            return View();
        }
        #endregion
        #region 删除操作
        public ContentResult Delete(Guid operationId)
        {
            permissionServiceImpl.DeleteOperation(operationId);
            return Content("ok");
        }
        #endregion
        #region 辅助显示
        /// <summary>
        /// 菜单关联资源
        /// </summary>
        /// <param name="operationId"></param>
        /// <param name="permissionId"></param>
        /// <returns></returns>
        public ContentResult OperationPermission(Guid operationId,Guid permissionId)
        {
            permissionServiceImpl.Update(operationId, permissionId);
            return Content("ok");
        }
        /// <summary>
        /// 前台由ligirueGird显示
        /// </summary>
        /// <returns></returns>
        public ActionResult GetJsonOfOperations()
        {
            var operations = permissionServiceImpl.GetAllOperations();
            if (operations != null && operations.Count() != 0)
            {
                var jsonResult = operations.Select(o=> new
                {
                    Key=o.Id,
                    isParent=o.ParentId==o.Id?0:1,
                    Code=o.Code,
                    Name=o.OperationName,
                    IconUrl=o.IconUrl,
                    Des=o.Desc,
                    isChild=o.ParentId==o.Id?"":"√",
                    MacthPermission = o.PermissionDto!=null?o.PermissionDto.Name:"",
                   // ParentId =o.ParentId
                   OrderNum= o.OrderNum,
                    ParentName = o.ParentId == o.Id ? o.OperationName : operations.Where(t=>t.Id==o.ParentId).FirstOrDefault().OperationName
                });

                var target = Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult);
               target = "{\"Rows\":" + target + ",\"Total\":\"10\"}";
               Response.Cache.SetCacheability(HttpCacheability.NoCache);
               return Content(target);
            }
            return Content("");
        }
        #endregion
    }
}
